﻿using Data.Model.SqlServer;
using DataService.Cache.Caching;
using DataService.Cache.IService;
using DataService.Cache.RedisService;
using DataService.Core.Common;
using DataService.Main;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DataService.Core.Json;

namespace UnitTest
{

    public class TestCache
    {
        #region 字典缓存
        private static DictionaryCache cache = new DictionaryCache()
        {
            Period = 20
        };
        public void SetDictionaryCache()
        {

            Console.WriteLine(cache.GetHashCode());
            Task task = new Task(() =>
            {

                cache.Set("测试1", "测试值啊啊啊", 120);
            });
            Task task1 = new Task(() =>
            {

                cache.Set("测试2", "测试值呵呵呵", 120);
            });
            Task task2 = new Task(() =>
            {

                cache.Set("测试3", "测试值黑恶黑hi", 19);
            });
            Task task3 = new Task(() =>
            {

                cache.Set("测试4", "测试值44444444444");
            });
            task.Start();
            task1.Start();
            task2.Start();
            task3.Start();
            Task.WaitAll(task, task1, task2, task3);
            Console.WriteLine("****************清理前*************");
            Console.WriteLine(cache.Get<string>("测试1"));
            Console.WriteLine(cache.Get<string>("测试2"));
            Console.WriteLine(cache.Get<string>("测试3"));
            Console.WriteLine(cache.Get<string>("测试4"));
            Console.WriteLine("****************清理前*************");

            Thread.Sleep(1000 * 36);
            Console.WriteLine("主线程：" + DateTime.Now);
            Console.WriteLine("****************清理后*************");
            Console.WriteLine("长度多少：" + cache.Count);
            foreach (var item in cache.Keys)
            {
                Console.WriteLine("Key：" + item);
            }

            Console.WriteLine(cache.Get<string>("测试1"));
            Console.WriteLine(cache.Get<string>("测试2"));
            Console.WriteLine(cache.Get<string>("测试3"));
            Console.WriteLine(cache.Get<string>("测试4"));
            Console.WriteLine("****************清理后*************");

            foreach (var item in cache.Keys)
            {
                Console.WriteLine("Key：" + item);
            }

            Console.WriteLine("****************保存文件*************");
            File.Delete(@"D:\资料相关\框架代码\DataService\UnitTest\Model\xxxxx.cache");
            cache.Save(@"D:\资料相关\框架代码\DataService\UnitTest\Model\xxxxx.cache", false);
            Console.WriteLine("****************保存文件*************");
            var dic = cache.LoadCache(@"D:\资料相关\框架代码\DataService\UnitTest\Model\xxxxx.cache", false);
            foreach (var item in dic)
            {
                Console.WriteLine("文件中加载Key：" + item.Key + "值：" + item.Value);
            }
            Console.WriteLine("完成");
        }

        public void TestCacheFile()
        {

            using (var cache = CacheDAL.DictionaryCacheInstance())
            {
                cache.Set<string>("11", " cess阿斯顿发顺丰健康科技98989345：1，87895", 120);//缓存120秒
                cache.Set<string>("22", " cess阿斯顿发顺丰健康科技98989345：1，87895");//缓存30秒
                cache.Set<string>("33", " cess阿斯顿发顺丰健康科技98989345：1，87895");//永不过其
                cache.Save(@"D:\资料相关\框架代码\DataService\UnitTest\Model\xxxxx.cache", false);

                #region 从缓存文件中加载缓存数据
                cache.Load(@"D:\资料相关\框架代码\DataService\UnitTest\Model\xxxxx.cache", false);
                Console.WriteLine(cache.Get<string>("11"));
                Console.WriteLine(cache.Get<string>("22"));
                Console.WriteLine(cache.Get<string>("33"));
                #endregion


                var dic = cache.LoadCache(@"D:\资料相关\框架代码\DataService\UnitTest\Model\xxxxx.cache", false);
            }
        }
        #endregion

        #region Redis



        public void TestSetRedis()
        {
            #region  string类型
            CacheDAL.Redising.Set<string>("r1", "第一个redis", 120);//缓存120秒
            CacheDAL.Redising.Set<string>("r2", "测试第二个值缓存30秒", 60);//缓存30秒
            CacheDAL.Redising.Set<string>("r7", "测试主从同步11111", 180);//缓存30秒
            CacheDAL.Redising.Set<string>("r8", "测试主从同步22222", 180);//缓存30秒
            CacheDAL.Redising.Set<string>("r9", "测试主从同步333333", 180);//缓存30秒
            #endregion

            #region hash类型
            //1.第一种方式；需要用Get<>取值，然后序列化
            Dictionary<string, object> dic1 = new Dictionary<string, object>();
            dic1["k1_1"] = "k值";
            dic1["k1_2"] = new PersonnelInfo() { CertificateNames = "测试1", PersonnelName = "而是222" };
            CacheDAL.Redising.Set<Dictionary<string, object>>("r3", dic1);//永不过期

            //2.第二种方式；没有过期时间
            Dictionary<string, object> dic2 = new Dictionary<string, object>();
            dic2["k2_1"] = "测试批量保存1";
            dic2["k2_2"] = new PersonnelInfo() { CertificateNames = "测试1", PersonnelName = "而是222" };
            dic2["k2_3"] = 6;

            //2.1 redis中 如果相同的key名但是不同类型会报错，所以先删除
            CacheDAL.Redising.Remove("r6");
            var hash = CacheDAL.Redising.GetDictionary<object>("r6") as RedisHash<String, object>;
            hash["k2_4"] = "测试追加";
            hash.HMSet(dic2);
            #endregion

            #region 列表类型
            //3.第一种，在redis里面，实际上是一个json字符串，所以需要用Get<>取值，然后序列化；熟悉redis结构很重要
            List<PersonnelInfo> list = new List<PersonnelInfo>();
            TestJson testCache1 = new TestJson();
            for (int i = 0; i < 30; i++)
            {
                list.Add(testCache1.GetModel());
            }
            CacheDAL.Redising.Set<List<PersonnelInfo>>("r4", list);

            //4.第二种；没有过期时间
            CacheDAL.Redising.Remove("r5");
            var l = CacheDAL.Redising.GetList<PersonnelInfo>("r5");
            var d = l as RedisList<PersonnelInfo>;
            d.AddRange(list);
            #endregion

        }

        public void TestGetRedis()
        {
            Console.WriteLine("********************读取r4集合字符串转换********************");
            string json = CacheDAL.RedisReading.Get<string>("r4");
            var list1 = json.ToJsonEntity<List<PersonnelInfo>>();
            foreach (var item in list1)
            {
                Console.WriteLine("json转换：" + item.CertificateNames);
            }
            Console.WriteLine("********************读取r4集合字符串转换********************");

            Console.WriteLine("********************读取r3 hash********************");
            var hashStr = CacheDAL.RedisReading.Get<string>("r3");
            var hashDic = hashStr.ToJsonEntity<Dictionary<string, object>>();
            foreach (var item in hashDic)
            {
                Console.WriteLine("r3 hash：" + item.Key + "**" + item.Value);
            }
            Console.WriteLine("********************读取r3 hash********************");

            Console.WriteLine("********************读取string类型********************");
            Console.WriteLine("r1 值：" + CacheDAL.RedisReading.Get<string>("r1"));
            Console.WriteLine("r2 值：" + CacheDAL.RedisReading.Get<string>("r2"));
            Console.WriteLine("********************读取string类型********************");

            Console.WriteLine("********************读取r5列表********************");
            var r5List = CacheDAL.RedisReading.GetList<PersonnelInfo>("r5");
            foreach (var item in r5List)
            {
                Console.WriteLine("r5 列表值：" + item.CertificateNames);
            }
            Console.WriteLine("********************读取r5列表********************");

            Console.WriteLine("********************测试Hash********************");
            var dic = CacheDAL.RedisReading.GetDictionary<object>("r6");
            foreach (var item in dic)
            {
                Console.WriteLine("r6 hash：" + item.Key + "****" + item.Value);
            }

            var hash = CacheDAL.Redising.GetDictionary<object>("r6") as RedisHash<String, object>;
           var allDic= hash.GetAll();
            Console.WriteLine("********************测试Hash********************");




        }


        public void TestRedisConfig()
        {
            //string configs = "server=127.0.0.1:6379,127.0.0.1:7001;password=695845;db=0";
            //for (int i = 0; i < 10; i++)
            //{
            //    using (FullRedis cache = CacheFactory.RedisInstance(configs))
            //    {
            //        Console.WriteLine(cache.GetHashCode());
            //    }
            //}

        }
        #endregion

        #region Redis主从
        public void TestReadSlave()
        {
            Console.WriteLine(CacheDAL.Redising.Get<string>("r3"));
        }
        #endregion

    }

}
